package aom.example.security.controller;

import aom.example.security.domain.SysRoleEntity;
import aom.example.security.domain.vo.RoleVo;
import aom.example.security.service.SysRoleService;
import bom.example.common.common.Results;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author X.I.O
 * @title: RoleController
 * @projectName springboot
 * @description: TODO
 * @date 2021/10/27 19:45
 */
@Api(tags = {"角色菜单"})
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("/roleList")
    @ApiOperation("角色分页列表")
    public JSONObject rolePageListController(@ApiParam("页码") @RequestParam("pageNum")Integer pageNum,
                                             @ApiParam("条数")@RequestParam("pageSize")Integer pageSize,
                                             @ApiParam("关键字")@RequestParam("keyword")String keyword){
        LambdaQueryWrapper<SysRoleEntity> lambdaQueryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(keyword)){
            lambdaQueryWrapper.or().like(SysRoleEntity::getRoleName,keyword);
        }
        lambdaQueryWrapper.orderByDesc(SysRoleEntity::getCreateDate);
        Page page=new Page(pageNum,pageSize);
        IPage<SysRoleEntity> sysRoleEntityIPage=sysRoleService.page(page,lambdaQueryWrapper);
        return Results.success(sysRoleEntityIPage.getRecords(),sysRoleEntityIPage.getTotal());
    }

    @ApiOperation("角色添加")
    @PostMapping("/addRole")
    public JSONObject roleAddController(@RequestBody RoleVo roleVo){
        SysRoleEntity sysRoleEntity=new SysRoleEntity();
        BeanUtils.copyProperties(roleVo,sysRoleEntity);
        sysRoleService.save(sysRoleEntity);
        return Results.success();
    }


    /**
     * 查询用户不具有的权限
     * @param uid
     * @return
     */
    @ApiOperation("角色下拉列表")
    @PostMapping("roleselect/{uid}")
    public JSONObject roleMapList(@ApiParam("用户id")@PathVariable("uid")String uid){
       List<Map<String, Object>> mapList=sysRoleService.selectUserNotRole(uid);
       JSONArray array=new JSONArray();
        for (int i = 0; i < mapList.size() ; i++) {
            Map<String, Object> map=mapList.get(i);
            JSONObject object=new JSONObject();
            String id=String.valueOf(map.get("role_id"));
            String roleName=String.valueOf(map.get("role_name"));
            object.put("key",id);
            object.put("value",roleName);
            array.add(object);;
        }
        return Results.success(array);
    }

    @ApiOperation("获取当前用户角色列表")
    @PostMapping("getuserrole/{uid}")
    public JSONObject getUserRoleListController(@ApiParam("用户id")@PathVariable("uid")String uid){
        List<Map<String, Object>> maps=sysRoleService.selectRoleUserByUserId(uid);
        return Results.success(maps);
    }

    @ApiOperation("添加角色通过用户id")
    @PostMapping("addroleuser")
    public JSONObject addUserRoleController(@ApiParam("用户id")@RequestParam("userId") String userId,
                                            @ApiParam("角色id")@RequestParam("roieId") String roieId){
        sysRoleService.insertSysUserRole(userId,roieId);
        return Results.success();
    }

    @ApiOperation("删除用户角色通过id")
    @PostMapping("deleteroleuser/{id}")
    public JSONObject deleteUserRoleByIdController(@PathVariable("id")String id){
        sysRoleService.deleteSysUserRoleById(id);
        return Results.success();
    }
}
